# Create your views here.

from django.http import HttpResponse
from django import forms
from django.template import Template, Context
from django.shortcuts import render_to_response
from computers.models import *
from django.views.decorators.csrf import *


class ContactForm(forms.Form):
    com_num = forms.CharField(max_length=100, label='Computer Number')
    symptom = forms.CharField(max_length=100, label='Symptom')
    date = forms.DateField(label='Date')
    names = forms.CharField(max_length=100, label='Name')


def addData(request):
    temp = 'add'
    form = ContactForm()
    return render_to_response('Datacomputer.html', locals())


@csrf_exempt
def Savedata(request):
    count_error = 0
    count_good = 0
    if request.POST['temp'] == 'add':
        com_num = request.POST['com_num']
        symptom = request.POST['symptom']
        if com_num and symptom:
            if not computer.objects.filter(comID=com_num):
                if symptom == 'error':
                    if request.POST['date'] == ''
                    or request.POST['names'] == '':
                        date = "Please enter Date"
                        names = 'Please Enter Name'
                        comError = HistoryComError(comID=com_num,
                        date=date, name=names)
                        comError.save()
                        com = computer(comID=com_num, status=symptom,
                        errorID_id=comError.id)
                        com.save()
                    else:
                        date = request.POST['date']
                        names = request.POST['names']
                        comError = HistoryComError(comID=com_num,
                        date=date, name=names)
                        comError.save()
                        com = computer(comID=com_num, status=symptom,
                        errorID_id=comError.id)
                        com.save()
                else:
                    com = computer(comID=com_num, status=symptom)
                    com.save()
                comError = computer.objects.filter(status='error')
                comGood = computer.objects.filter(status='good')
                for i in comError:
                    count_error = count_error + 1
                for j in comGood:
                    count_good = count_good + 1
                countCom.objects.all().delete()
                count = countCom(com_good=count_good, com_error=count_error)
                count.save()
    else:
        ids = request.POST['x']
        computer_id = computer.objects.get(id=ids)
        computer_id.comID = request.POST['com_num']
        computer_id.status = request.POST['symptom']
        if computer_id.status == 'error':
            dateComError = HistoryComError.objects.filter
            (comID=computer_id.comID).filter(date=request.POST['date'])
            if HistoryComError.objects.filter(comID=computer_id.comID)
            .filter(date=request.POST['date']):
                for i in dateComError:
                    if i.date != request.POST['date']:
                        if request.POST['date'] == '' or request.POST['names']
                        == ''or request.POST['date'] == '-'
                        or request.POST['names'] == '-':
                            return render_to_response('DateName.html',
                            locals())
                        else:
                            comError = HistoryComError(comID=computer_id.comID,
                            date=request.POST['date'],
                            name=request.POST['names'])
                            comError.save()
                            computer_id.errorID_id = comError.id
                            computer_id.save()
            else:
                if request.POST['date'] == '' or request.POST['names'] ==
                ''or request.POST['date'] == '-'
                or request.POST['names'] == '-':
                    return render_to_response('DateName.html', locals())
                else:
                    comError = HistoryComError(comID=computer_id.comID,
                    date=request.POST['date'], name=request.POST['names'])
                    comError.save()
                    computer_id.errorID_id = comError.id
                    computer_id.save()
        else:
            computer_id.save()
        comError = computer.objects.filter(status='error')
        comGood = computer.objects.filter(status='good')
        for i in comError:
            count_error = count_error + 1
        for j in comGood:
            count_good = count_good + 1
        countCom.objects.all().delete()
        count = countCom(com_good=count_good, com_error=count_error)
        count.save()
    computer_list = computer.objects.all()
    count_list = countCom.objects.all()
    computerError = HistoryComError.objects.all()
    return render_to_response('Showdata.html', locals())


@csrf_exempt
def computeredit(request, ids):
    temp = 'edit'
    computer_id = computer.objects.get(id=ids)
    if computer_id.status == 'error':
        form = ContactForm(initial={'com_num': computer_id.comID,
        'symptom': computer_id.status,
        'date': computer_id.errorID.date,
        'names': computer_id.errorID.name})
    else:
        form = ContactForm(initial={'com_num': computer_id.comID,
        'symptom': computer_id.status, 'date': '-', 'names': '-'})
    return render_to_response('Datacomputer.html', locals())


def Showdata(request):
    computer_list = computer.objects.all().order_by("comID")
    count_list = countCom.objects.all()
    return render_to_response('Showdata.html', locals())


def Showhistory(request):
    computerError = HistoryComError.objects.all().order_by("comID")
    return render_to_response('Showhistory.html', locals())
